package com.dq.controller.user.centre;

import com.dq.controller.in.user.UserPlanQuery;
import com.dq.domain.org.Org;
import com.dq.domain.sys.user.user.centre.UserPlan;
import com.dq.exception.customException.AdminException;
import com.dq.service.AllService;
import com.dq.utils.DateUtils;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author cf
 */

@RestController
@RequestMapping("/sys/userPlan/")
@Api(description = "工作计划管理")
public class SysUserPlanController extends AllService {

    @PostMapping("list")
    @ApiOperation("列表")
    public Vo customerList(@Valid @RequestBody UserPlanQuery userPlanQuery)throws AdminException {
        userPlanQuery.format();

        List<UserPlan.Vo> voList = new ArrayList<>();
        List<UserPlan> list = userPlanService.list(userPlanQuery);
        for (UserPlan userPlan : list) {
            UserPlan.Vo vo = (UserPlan.Vo) userPlan.toVO();
            Map<String, Object> map = userSummaryService.completeMap(userPlan.getBossUser().getId(), userPlan.getTarget());
            vo.setFinish(new BigDecimal(map.get("completedByMonth").toString()));
            vo.setResidue(new BigDecimal(map.get("incomplete").toString()));
            voList.add(vo);
        }

        return new ListDataVo(voList, userPlanService.count(userPlanQuery));
    }

    @PostMapping("add")
    @ApiOperation("添加")
    public Vo add(@RequestBody UserPlan userPlan)throws AdminException {
        userPlanService.save(userPlan);

        return new Vo(HttpStatus.OK);
    }
    @PostMapping("edit")
    @ApiOperation("修改")
    public Vo edit(@RequestBody UserPlan userPlan)throws AdminException{
        userPlanService.update(userPlan);
        return new Vo(HttpStatus.OK);
    }

}
